<?
class ccdsAnalytics
	extends ccdsBackendPlugin
{
	private $name = "ccdsAnalytics";
	private $aExpand = array();
	private $percent = 1;
	private $inc = 1;

	private $objResponse = false;
	function __construct()
	{
		parent::__construct();


		$this->xajax = ccdsXajax::getInstance();

		$this->xajax->register( XAJAX_CCDS_FUNCTION, "AnalyticsOpen", $this->name );		
		
	}

	function AnalyticsOpen($aFormValues) 
	{

		$objResponse = new ccdsResponse();

		if ( "" == $aFormValues['project_id'] )
		{
			$objResponse->alert("Fehler: project_id fehlt");
			return $objResponse;
			
		}
		
		$icon_src = $this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/chart_bar.png";

		$objResponse->loadcommands( $this->tools->addTabManager( 'topTab', 'Analytics_'.$aFormValues['project_id'], "<img src=\"".$icon_src."\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">".$obj['obj_name_name']."</span>" ) );


		/* new overview panel */
		$objResponse->clsTabManager->addPanel( 'TabManagerContent_Analytics_'.$aFormValues['project_id'], array
		(
			"id" => "GAoverviewPanel_".$aFormValues['project_id'],
			"title" => "<img src=\"".$this->config->get( 'LIBRARY_WEB' )."famfamfam/icons/application.png\" style=\"width:16px\" border=\"0\" align=\"left\" valign=\"top\" /><span style=\"margin-left:4px;\">&Uuml;bersicht</span>",
			"content" => "<div id='GAoverview_".$aFormValues['project_id']."'></div>",
			"closeable" => false
		));


		$data = $this->parse_xml();


		$objResponse->script("
try {
	var api = new jGCharts.Api(); 
	jQuery('<img>') 
	.attr('src', api.make({ 
		data : ".json_encode($data['values']).",
		type:'lc',
		size:'700x300',
		axis_labels : ".json_encode($data['labels'])."

	})) .appendTo('#GAoverview_".$aFormValues['project_id']."');
} catch(ex) {alert(ex);}	
		");	
		
/*
[[105.7,97.9],[108.1,101.6],[110.7,102.9],[111.0,93.7],[110.0,89.8],[109.0,90.7], 
		[107.5,93.0],[106.1,94.5],[104.3,91.9],[102.0,93.9],[102.8,93.6],[103.8,92.6], 
		[102.9,94.0],[102.1,92.7],[100.6,96.0],[101.7,97.9],[101.8,105.0], 
		[103.3,104.1],[104.0,105.1],[103.7,108.1],[108.4,108.4],[109.4,113.8], 
		[112.0,109.1],[112.6,106.3],[115.5,106.7],[115.7,108.8],[114.7,118.8], 
		[115.9,120.4],[116.2,115.9],[118.0,124.7],[123.3,126.5],[127.6,131.6], 
		[130.3,134.0],[135.5,135.7],[138.2,126.4],[139.6,127.4],[145.1,131.0], 
		[146.4,129.9],[147.1,133.7],[149.0,138.4],[150.3,141.0],[151.3,139.3], 
		[153.4,145.3],[152.7,142.9],[152.9,129.2],[152.2,126.0],[151.9,124.8], 
		[150.1,125.9],[148.2,118.9],[145.3,122.9],[142.9,127.7],[142.6,134.4], 
		[144.0,138.5],[145.5,138.7],[147.2,141.8],[150.0,139.2],[153.8,145.6], 
		[155.4,147.6],[157.0,157.9],[158.4,156.2],[162.8,153.9],[162.8,158.6], 
		[164.7,166.3],[168.5,165.8]]*/

		return $objResponse;
	}
	
	
	private function parse_xml () 
	{

	$result = array();
	$file = "/var/www/web3/html/_sandbox/google.de/traffic_report.xml";
	$xml = new ccdsXMLparser($file);
	$data = $xml->getArray();
	
	$result['labels'] = array();
	$result['values'] = array();

	$i=0;
	foreach ($data['Report']['Graph']['Serie']['Point'] as $point) {
		$value = (int) str_replace('.','',$point['Value']);
		$label = is_int($i/4) ? $point['Label'] : ' ';
		$result['labels'][]=$label;
		$result['values'][]=$value;
		$i++;
	}
	
	
	return $result;
		
		
		
	}

	
}